package com.lijianan.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lijianan.entity.MeetingRoomEquipment;
import com.lijianan.entity.MultimediaEquipment;
import com.lijianan.mapper.MeetingRoomEquipmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MeetingRoomEquipmentServiceImpl implements MeetingRoomEquipmentService {

    @Autowired
    private MeetingRoomEquipmentMapper meetingRoomEquipmentMapper;

    @Autowired
    private MultimediaEquipmentServiceImpl multimediaEquipmentServiceImpl;

    @Override
    public List<MultimediaEquipment> selectMultimediaEquipmentsByRoomId(Integer room_id) {
        List<MultimediaEquipment> multimediaEquipments = new ArrayList<>();
        List<MeetingRoomEquipment> meetingRoomEquipments = selectByMeetingRoomId(room_id);
        for (MeetingRoomEquipment meetingRoomEquipment : meetingRoomEquipments) {
            MultimediaEquipment multimediaEquipment = multimediaEquipmentServiceImpl.selectByEquipmentId(meetingRoomEquipment.getEquipment_id());
            multimediaEquipments.add(multimediaEquipment);
        }
        return multimediaEquipments;
    }

    @Override
    public List<MeetingRoomEquipment> selectByMeetingRoomId(Integer room_id) {
        QueryWrapper<MeetingRoomEquipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id", room_id);
        return meetingRoomEquipmentMapper.selectList(queryWrapper);
    }

    @Override
    public MeetingRoomEquipment selectById(Integer id) {
        return meetingRoomEquipmentMapper.selectById(id);
    }

    @Override
    public List<MeetingRoomEquipment> selectAll() {
        return meetingRoomEquipmentMapper.selectList(null);
    }

    @Override
    public boolean addMeetingRoomEquipment(Integer room_id, Integer equipment_id) {
        QueryWrapper<MeetingRoomEquipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id", room_id).eq("equipment_id", equipment_id);
        if (meetingRoomEquipmentMapper.selectOne(queryWrapper) != null) {
            System.out.println("该会议室已经存在该设备！");
            return false;
        }
        MeetingRoomEquipment meetingRoomEquipment = new MeetingRoomEquipment();
        meetingRoomEquipment.setRoom_id(room_id);
        meetingRoomEquipment.setEquipment_id(equipment_id);
        return meetingRoomEquipmentMapper.insert(meetingRoomEquipment) != 0;
    }

    @Override
    public boolean updateMeetingRoomEquipment(Integer room_id, String name) {
        MultimediaEquipment multimediaEquipment = multimediaEquipmentServiceImpl.selectByName(name);
        return addMeetingRoomEquipment(room_id, multimediaEquipment.getEquipment_id());
    }

    @Override
    public boolean deleteEquipmentByRoomId(Integer room_id) {
        List<MeetingRoomEquipment> meetingRoomEquipments = selectByMeetingRoomId(room_id);
        for (MeetingRoomEquipment meetingRoomEquipment : meetingRoomEquipments) {
            if (!deleteMeetingRoomEquipment(meetingRoomEquipment.getId()))
                return false;
        }
        return true;
    }

    @Override
    public boolean deleteMeetingRoomEquipment(Integer id) {
        return meetingRoomEquipmentMapper.deleteById(id) != 0;
    }
}
